<template>
  <div id="contain">
    <div>
      <el-table
        :data="tableList"
        style="width: 945px"
        stripe
        :row-style="rowStyle"
      >
        <el-table-column prop="name" label="线路名" width="180" align="center">
        </el-table-column>

        <el-table-column
          prop="startAddr"
          label="起点"
          align="center"
        ></el-table-column>
        <el-table-column prop="endAddr" label="终点" align="center">
        </el-table-column>
        <el-table-column prop="adNum" label="成人数量" align="center">
        </el-table-column>
        <el-table-column prop="chNum" label="孩童数量" align="center">
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center" width="250">
          <template slot-scope="scope">
            <el-button type="mini" style="color: red" @click="clear(scope.row)"
              >取消订单</el-button
            >
            <el-button type="mini" @click="edit(scope.row)">修改</el-button>
            <el-button type="mini" @click="zhifu(scope.row)">去支付</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="服务名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="服务线路" :label-width="formLabelWidth">
          <el-input v-model="form.route" disabled></el-input>
        </el-form-item>
        <el-form-item label="服务起点" :label-width="formLabelWidth">
          <el-input v-model="form.startAddr" disabled></el-input>
        </el-form-item>
        <el-form-item label="服务终点" :label-width="formLabelWidth">
          <el-input v-model="form.endAddr" disabled></el-input>
        </el-form-item>
        <el-form-item label="儿童数量" :label-width="formLabelWidth">
          <el-input-number
            v-model="form.chNum"
            :min="0"
            :max="10"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="成人数量" :label-width="formLabelWidth">
          <el-input-number
            v-model="form.adNum"
            :min="0"
            :max="10"
          ></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-button
            style="float: right; margin: 0 15px"
            @click="dialogFormVisible = false"
            >取 消</el-button
          >
          <el-button type="primary" @click="submit2" style="float: right"
            >确 定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      token: window.localStorage.getItem("token"),
      tableList: [],
      form: {},
      formLabelWidth: "120px",
      dialogFormVisible: false,
      goPay: false,
    };
  },
  mounted() {
    this.fn();
  },
  beforeRouteLeave(to, from, next) {
    console.log(to);
    if (this.goPay) {
      localStorage.setItem("query", JSON.stringify(from.query));
      localStorage.setItem("path", from.path);
      next();
    } else {
      // next();
    }
  },
  methods: {
    fn() {
      this.$axios({
        method: "post",
        url: "/order/list/current",
        headers: { token: this.token },
        data: { type: 1 },
      }).then((res) => {
        if (res.data.code === 0) {
          this.tableList = [];
          let list = res.data.page;
          list.forEach((item) => {
            if (item.status === 0) {
              item.status = "未支付";
              this.tableList.push(item);
            }
          });
        }
      });
    },
    rowStyle() {
      return { height: "60px" };
    },
    edit(k) {
      //   console.log(k);
      this.dialogFormVisible = true;
      this.form = k;
    },

    clear(k) {
      this.$axios({
        method: "post",
        url: "/order/cancel",
        headers: { token: this.token },
        data: { orderId: k.orderId },
      }).then((res) => {
        if (res.data.code === 0) {
          this.$message({
            type: "success",
            message: "取消订单成功！",
          });
          this.fn();
        } else {
          this.$message({
            type: "error",
            message: res.data.msg,
          });
        }
      });
    },
    zhifu(k) {
      //   console.log(k);
      let data = {
        out_trade_no: k.orderId + "",
        subject: k.name,
        total_amount: k.pay,
      };
      this.goPay = true;
      localStorage.setItem('path',this.$route.path)
      localStorage.setItem('query',JSON.stringify(this.$route.query))
      //   console.log(data);
      this.$router.push({ path: "zhifu", query: data });
    },
    submit2() {
      this.dialogFormVisible = false;
      this.$axios({
        method: "post",
        url: "/order/edit",
        headers: { token: this.token },
        data: {
          orderId: this.form.orderId + "",
          chNum: this.form.chNum,
          adNum: this.form.adNum,
        },
      }).then((res) => {
        if (res.data.code === 500) {
          this.dialogFormVisible = false;
          this.$message({
            type: "error",
            message: res.data.msg,
          });
        }
        if (res.data.code === 0) {
          this.$message({
            type: "success",
            message: "修改成功！",
          });
          this.fn();
        }
      });
    },
  },
};
</script>
<style scoped></style>>
